<template>
  <div class="container">
    <div class="app-container">
      <!-- 一级权限 在根节点下添加权限 所以pid为0 -->
      <el-button size="mini" type="primary" class="buttonClass" @click="addPerssion(0 , 1)">添加权限</el-button>
      <el-table default-expand-all :data="list" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template v-slot="{ row }">
            <!-- 二级权限 在当前节点下新增 -->
            <el-button v-if="row.type === 1" size="mini" type="text" @click="addPerssion(row.id , 2)">添加</el-button>
            <el-button size="mini" type="text" @click="editPerssion(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delPerssion(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="`${showText}权限`" :visible.sync="showDialog" @close="btnCancel">
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
        <el-form-item prop="name" label="权限名称">
          <el-input v-model="formData.name" size="mini" style="width: 90%;" />
        </el-form-item>
        <el-form-item prop="code" label="权限标识">
          <el-input v-model="formData.code" size="mini" style="width: 90%;" />
        </el-form-item>
        <el-form-item prop="description" label="权限描述">
          <el-input v-model="formData.description" type="text" size="mini" style="width: 90%;" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0" />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="btnOk">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { addPerssion, getPerssionList, getPerssionListDetail, updatePerssion, delperssion } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: '0' // 默认关闭
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '新增'
    }
  },
  created() {
    this.getPerssionList()
  },
  methods: {
    async getPerssionList() {
      this.list = transListToTreeData(await getPerssionList(), 0)
    },
    // 新增权限方法
    addPerssion(pid, type) {
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },
    // 确定方法
    btnOk() {
      this.$refs.formRef.validate().then(() => {
        // 有id 是编辑操作
        if (this.formData.id) {
          return updatePerssion(this.formData)
        }
        // 没有id 是新增操作
        return addPerssion(this.formData)
      }).then(() => {
        // 提示消息
        this.$message.success('新增成功')
        this.getPerssionList()
        this.showDialog = false
      })
    },
    // 取消方法
    btnCancel() {
      // 后台数据置空
      this.formData = {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: '0' // 默认关闭
      }
      // 重置表单校验并清空数据
      this.$refs.formRef.resetFields()
      this.showDialog = false
    },
    // 编辑方法 根据id 获取权限详情
    async editPerssion(id) {
      this.formData = await getPerssionListDetail(id)
      this.showDialog = true
    },
    // 删除权限列表方法
    async delPerssion(id) {
      try {
        await this.$confirm('确定删除该数据吗')
        await delperssion(id)
        this.getPerssionList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
<style scoped lang="scss">
  .app-container{
    .buttonClass{
      margin: 10px;
    }
}
</style>
